package com.evendai.sipdemo

import android.annotation.SuppressLint
import android.content.Intent
import android.media.AudioManager
import android.os.Message
import com.evendai.sipdemo.databinding.ActivityMainBinding
import com.evendai.sipdemo.sip.audio.IncomingService
import com.evendai.sipdemo.sip.audio.OutgoingService
import com.evendai.sipdemo.sip.helper.EngineHelper
import com.evendai.sipdemo.util.SipUtil
import com.evendai.sipdemo.util.Utils
import org.doubango.Whats
import org.doubango.ngn.NgnEngine
import org.doubango.ngn.media.NgnProxyAudioConsumer
import org.doubango.ngn.media.NgnProxyAudioProducer
import org.doubango.ngn.media.NgnProxyPluginMgr
import org.greenrobot.eventbus.EventBus

class MainActivity : BaseActivity() {

    val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater, getContentContainer(), true) }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        supportActionBar?.hide()
        Utils.initMainActivityViews(this)
    }

    @Suppress("BooleanLiteralArgument")
    @SuppressLint("SetTextI18n")
    override fun initListener() {
        Utils.setMainActivityEditTextInputListener(this)

        binding.btnCallAudio.setOnClickListener { SipUtil.callAudio(this, isAudioMeeting = false) }       // 单呼语音
        binding.btnCallAudioMeeting.setOnClickListener { SipUtil.callAudio(this, isAudioMeeting = true) }  // 语音会议

        binding.btnCallVideo.setOnClickListener { SipUtil.callVideo(this, isVideoMeeting = false, isMainVideoMeeting = false)  }      // 单呼视频
        binding.btnCallVideoMeeting.setOnClickListener { SipUtil.callVideo(this, isVideoMeeting = true, isMainVideoMeeting = false)  } // 视频会议
        binding.btnCallMainVideoMeeting.setOnClickListener { SipUtil.callVideo(this, isVideoMeeting = false, isMainVideoMeeting = true)  }  // 主摄像头视频会议

        binding.btnInviteAudioMeeting.setOnClickListener { SipUtil.inviteMeeting(true) }
        binding.btnInviteVideoMeeting.setOnClickListener { SipUtil.inviteMeeting(false) }

        // 打开扬声器
        binding.btnOpenSpeaker.setOnClickListener {
            EventBus.getDefault().post(Message.obtain(null, Whats.SET_SPEAKERPHONE_ON, true))
            Constants.sSpeakerphoneOn = true
        }

        // 关闭扬声器
        binding.btnCloseSpeaker.setOnClickListener {
            EventBus.getDefault().post(Message.obtain(null, Whats.SET_SPEAKERPHONE_ON, false))
            Constants.sSpeakerphoneOn = false
        }

        binding.btnOpenMicrophone.setOnClickListener { NgnProxyAudioProducer.sOnMute = false }     // 开麦克风
        binding.btnCloseMicrophone.setOnClickListener { NgnProxyAudioProducer.sOnMute = true }     // 关麦克风

        // 发送sip消息
        binding.btnSendSipMessage.setOnClickListener {
            SipUtil.sendSipMessage(SipUtil.makeValidSipUri(PersistentVariables.outgoingNumber), PersistentVariables.sipMessage)
        }

        // 注销
        binding.btnLogout.setOnClickListener {
            setLoadingText("正在注销")
            showLoading()
            PersistentVariables.isLogout = true
            EngineHelper.stopEngine() {
                hideLoading()
                finish()
            }
        }

    }

    override fun initData() {
        volumeControlStream = AudioManager.STREAM_VOICE_CALL // 设置音量控制为电话声音控制
        if (NgnEngine.getInstance() != null) {
            NgnProxyPluginMgr.startAudioCollection()
        }
        startService(Intent(this, IncomingService::class.java))
    }

    override fun onDestroy() {
        super.onDestroy()
        if (NgnEngine.getInstance() != null) {
            NgnProxyPluginMgr.stopAudioCollection()
        }
        stopService(Intent(this, IncomingService::class.java))
        stopService(Intent(this, OutgoingService::class.java))
    }

    override fun onBackPressed() {
        moveTaskToBack(false) // 把应用后台运行，参数: 当Activity是栈里的根时用false，否则用true
    }
}